﻿using Application.Initialize.Models;
using Application.Share.Event;
using Prism.Events;

namespace Application.Initialize.Services
{
    /// <summary>
    /// 硬件生命周期管理器
    /// </summary>
    public class HardwareManager
    {
        private IEventAggregator EventAggregator { get; }
        /// <summary>
        /// 表示硬件是否已经初始化完成
        /// </summary>
        public bool Initialized { get; set; }

        public HardwareManager(IEventAggregator eventAggregator)
        {
            EventAggregator = eventAggregator;
            Initialized = false;
        }
        public async Task<HardwareInitializeResult> InitAsync()
        {
            if (Initialized)throw new Exception ("Hardware already initialized");
            //to do:初始化硬件

            await Task.Delay(2000);//模拟硬件初始化耗时

            Initialized = true;

            EventAggregator.GetEvent<InitializedSuccessEvent>().Publish();//发布硬件初始化完成事件

            return new HardwareInitializeResult { Success = true, Message = "硬件初始化成功" };
        }
    } 
}
